package com.example.demo.Service.impl;

import com.example.demo.Dao.UserDao;
import com.example.demo.entity.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnswerServiceImpl implements com.example.demo.Service.AnswerService {
    @Autowired
    private com.example.demo.Dao.AnswerDao answerDao;
    @Autowired
    private UserDao userDao;

    @Override
    public List<Answer> SelectAll() {
        return answerDao.SelectAllAnswer();
    }

    @Override
    public int DisableAnswer(Answer answer) {
        return answerDao.DisableAnswer(answer);
    }

    @Override
    public int AbleAnswer(Answer answer) {
        return answerDao.AbleAnswer(answer);
    }

    @Override
    public int EditAnswer(Answer answer) {
        // 修改分数的时候，重新计算回答质量分数
        answerDao.EditAnswerGrade(answer);
        Answer entity = answerDao.SelectById(Integer.parseInt(answer.getId()));
        String userName = entity.getUserName();
        float answerQualityScore = answerDao.sumByUserName(userName);
        int count = answerDao.countByUserName(userName);
        answerQualityScore = answerQualityScore / count;
        return userDao.updateAnswerQualityScore(userName, answerQualityScore);
    }

    @Override
    public int InsertAnswerByUser(Answer answer) {
        return answerDao.InsertAnswerByUser(answer);
    }
}
